package com.shared.controller;

import com.shared.dao.*;
import com.shared.pojo.*;
import com.shared.server.PCouponServer;
import com.shared.server.POrdersServer;
import com.shared.util.RestDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
public class OrderController {
    @Autowired
    private PGoodsMapper pGoodsMapper;
    @Autowired
    private PAddressMapper pAddressMapper;
    @Autowired
    private PCouponMapper pCouponMapper;
    @Autowired
    private PGoodsCartMapper pGoodsCartMapper;
    @Autowired
    private POrdersServer pOrdersServer;
    @Autowired
    private POrdersMapper pOrdersMapper;
    @Autowired
    private PCouponServer pCouponServer;
    @Autowired
    private POrderItemMapper pOrderItemMapper;

    @PostMapping("/order/{id}/{addressid}/{couponid}")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "添加订单信息")
    public RestDTO order(@PathVariable("id") Integer id,
                         @PathVariable("addressid") Integer addressid,
                         @PathVariable("couponid") Object couponid) {
        PGoods pGoods = pGoodsMapper.queryById(id);
        PAddress pAddress = pAddressMapper.queryById(addressid);
        PCoupon pCoupon = new PCoupon();
        if (couponid != null) {
            pCoupon = pCouponMapper.queryById(couponid);
        } else {
            pCoupon = null;
        }
        Map data = new HashMap<>();
        data.put("pGoods", pGoods);
        data.put("pAddress", pAddress);
        data.put("pCoupon", pCoupon);
        String orderno = (UUID.randomUUID().toString());
        POrders pOrders = new POrders();
        pOrders.setOrdernsn(orderno);
        pOrders.setUserid(1);
        pOrders.setGoodsprices(pGoods.getPrice());
        pOrders.setUsername(pAddress.getConsignee());
        pOrders.setUserphone(pAddress.getPhone());
        pOrders.setUseraddress(pAddress.getAddress() + pAddress.getArea() + pAddress.getNum());
        if (pCoupon != null) {
            pOrders.setCouponid(pCoupon.getId());
        } else {
            pOrders.setCouponid(null);
        }
        if (pCoupon != null && pCoupon.getCriteria() <= pGoods.getPrice()) {
            pOrders.setCoupons(pCoupon.getAmount());

        } else {
            pOrders.setCoupons(0);
        }
        pOrders.setTotalamout((int) (pGoods.getPrice() - pOrders.getCoupons()));
        pOrders.setState(0);
        pOrders.setFoundtime(new Date());
        if (pGoods.getStock() > 0) {
            try {
                this.pOrdersServer.addOrder(pOrders);
            } catch (Exception e) {
                return RestDTO.ADD_FAILURE;
            }
        }
        POrderItem pOrderItem = new POrderItem();
        pOrderItem.setOrderid(pOrders.getId());
        pOrderItem.setGoodsid(id);
        pOrderItem.setOrdersn(orderno);
        if (pGoods.getStock() > 0) {
            pOrderItem.setGoodsnum(1);
        } else {
            return RestDTO.ADD_FAILURE;
        }
        pOrderItem.setGoodspic(pGoods.getPic());
        pOrderItem.setGoodsprice(pGoods.getPrice());
        pOrderItem.setGoodstitle(pGoods.getTitle());
        try {
            this.pOrdersServer.assOrderItem(pOrderItem);
            this.pOrdersServer.updateCountById(id, -1);
            this.pCouponMapper.uodateCoupon(pOrders.getCouponid(), 1);
            RestDTO restDTO = new RestDTO();
            restDTO.setData(pOrders.getOrdernsn());
            return restDTO;
        } catch (Exception e) {
            return RestDTO.ADD_FAILURE;
        }
    }

    @PostMapping("/orders/{addressid}/{couponid}/{goodsprice}")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "添加订单信息多个")
    public RestDTO inenr(@PathVariable("addressid") Integer addressid,
                         @PathVariable("couponid") Object couponid,
                         @PathVariable("goodsprice") Integer goodsprice,
                         @RequestBody Integer[] goodsid) {
        PAddress pAddress = pAddressMapper.queryById(addressid);
        PCoupon pCoupon = new PCoupon();
        if (couponid != null) {
            pCoupon = pCouponMapper.queryById(couponid);
        } else {
            pCoupon = null;
        }
        String orderno = UUID.randomUUID().toString();
        POrders pOrders = new POrders();
        pOrders.setOrdernsn(orderno);
        pOrders.setUsername(pAddress.getConsignee());
        pOrders.setUserphone(pAddress.getPhone());
        pOrders.setUseraddress(pAddress.getAddress() + pAddress.getArea() + pAddress.getNum());
        pOrders.setUserid(1);
        pOrders.setState(0);
        pOrders.setGoodsprices((double) goodsprice);
        pOrders.setFoundtime(new Date());
        if (pCoupon != null) {
            pOrders.setCouponid(pCoupon.getId());
            this.pCouponMapper.uodateCoupon(pOrders.getCouponid(), 1);
        } else {
            pOrders.setCouponid(null);
        }
        if (pCoupon != null && pOrders.getGoodsprices() > pCoupon.getCriteria()) {
            pOrders.setCoupons(pCoupon.getAmount());
        } else {
            pOrders.setCoupons(0);
        }
        pOrders.setTotalamout((int) (pOrders.getGoodsprices() - pOrders.getCoupons()));
        this.pOrdersServer.addOrder(pOrders);
        for (Integer a : goodsid) {
            System.out.println(a);
            PGoodsCart pGoodsCart = pGoodsCartMapper.queryById(a);
            POrderItem pOrderItem = new POrderItem();
            pOrderItem.setOrdersn(orderno);
            pOrderItem.setOrderid(pOrders.getId());
            pOrderItem.setGoodsid(pGoodsCart.getGoodsid());
            pOrderItem.setGoodstitle(pGoodsCart.getName());
            pOrderItem.setGoodspic(pGoodsCart.getImg());
            pOrderItem.setGoodsprice(pGoodsCart.getPrice());
            pOrderItem.setGoodsnum(pGoodsCart.getNumber());
            this.pOrdersServer.assOrderItem(pOrderItem);
            this.pGoodsCartMapper.deleteCartById(a);
        }
        RestDTO restDTO = new RestDTO();
        restDTO.setData(pOrders.getOrdernsn());
        return restDTO;
    }

    @GetMapping("/orders/{userid}")
    @ResponseBody
    @ApiOperation(value = "根据用户id查询订单信息")
    public List<POrders> getOrders(@PathVariable("userid") Integer userid) {
        return pOrdersServer.findOrderByUserid(userid);
    }

    @GetMapping("/uuen/{userid}")
    @ResponseBody
    @ApiOperation(value = "根据用户id查询订单详情信息")
    public List<POrderItem> ssa(@PathVariable("userid") Integer userid) {

        return pOrdersMapper.findOrderitem(userid);
    }


    @GetMapping("/orderpice/{orderno}")
    @ResponseBody
    @ApiOperation(value = "根据订单号查询订单信息")
    public List<POrders> findorder(@PathVariable("orderno") String orderno) {
        return pOrdersServer.findorderByOrderno(orderno);
    }


    @GetMapping("/orderno/{orderno}")
    @ResponseBody
    @ApiOperation(value = "根据订单号查询订单信息，修改订单状态")
    public RestDTO orderstate(@PathVariable("orderno") String orderno) {
        POrders pOrders = pOrdersMapper.findorders(orderno);
        this.pOrdersServer.updateOrderState(orderno, 1);
        List<POrderItem> pOrderItems = pOrderItemMapper.findByOrdersn(orderno);
        for (POrderItem pOrderItem : pOrderItems) {
            this.pGoodsMapper.updatesalesById(pOrderItem.getGoodsid(), pOrderItem.getGoodsnum());
        }
        RestDTO restDTO = new RestDTO();
        if (pOrders.getGoodsprices() >= 100 && pOrders.getGoodsprices() < 300) {
            this.pCouponServer.addCoupon1();
            restDTO.setCode(100);
            return restDTO;
        }
        if (pOrders.getGoodsprices() >= 300 && pOrders.getGoodsprices() < 500) {
            this.pCouponServer.addCoupon2();
            restDTO.setCode(300);
            return restDTO;
        }
        if (pOrders.getGoodsprices() >= 500 && pOrders.getGoodsprices() < 1000) {
            this.pCouponServer.addCoupon3();
            restDTO.setCode(500);
            return restDTO;
        }
        if (pOrders.getGoodsprices() > 1000) {
            this.pCouponServer.addCoupon4();
            restDTO.setCode(1000);
            return restDTO;
        }
        return null;
    }

    @GetMapping("/cuihuo/{ordernsn}")
    @ResponseBody
    @ApiOperation(value = "根据订单编号修改出货状态")
    public void cuihuo(@PathVariable("ordernsn") String ordernsn) {
        this.pOrdersMapper.updateOrderState(ordernsn, 2);
    }

    @GetMapping("/queren/{ordernsn}")
    @ResponseBody
    @ApiOperation(value = "根据订单编号修改收货状态")
    public void queren(@PathVariable("ordernsn") String ordernsn) {
        this.pOrdersMapper.updateOrderState(ordernsn, 3);
    }

    @GetMapping("/quexiao/{ordernsn}")
    @ResponseBody
    @ApiOperation(value = "根据订单编号修改取消状态")
    public RestDTO quexiao(@PathVariable("ordernsn") String ordernsn) {
        if (ordernsn != null) {
            this.pOrdersMapper.updateOrderState(ordernsn, 5);
            return RestDTO.UPDATE_SUCCESS;
        }
        return RestDTO.UPDATE_FAILURE;
    }
}
